package com.hudson.store.index;

import com.hudson.store.dispatch.DispatchDto;

import java.io.File;
import java.util.Map;

public class IndexService {
    private static IndexFile defaultIndexFile;

    static {
        defaultIndexFile = new IndexFile("store" + File.separator + "index" + File.separator + "indexFile");
    }

    public static void putIndex(DispatchDto dispatchDto) {
        defaultIndexFile.putKey(getKey(dispatchDto.getTopic(), dispatchDto.getOpaque()), dispatchDto.getCommitOffset(), dispatchDto.getMessageSize(), dispatchDto.getStoreTimeStamp());
        defaultIndexFile.putKey(getKey(dispatchDto.getTopic(), dispatchDto.getTag()), dispatchDto.getCommitOffset(), dispatchDto.getMessageSize(), dispatchDto.getStoreTimeStamp());
    }

    public static String getKey(String topic, String tag) {
        return topic + "#" + tag;
    }

    public static String getKey(String topic, long opaque) {
        return topic + "#" + opaque;
    }

    public static Map<Long, Integer> getIndex(String topic, String tag, int size) {
        return defaultIndexFile.getIndex(getKey(topic, tag), size);
    }

    public static Map<Long, Integer> getIndex(String topic, long opaque, int size) {
        return defaultIndexFile.getIndex(getKey(topic, opaque), size);
    }
}
